package com.woniuxy.springbootrabbitmq.controller;


import com.woniuxy.entity.Student;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

/**
 * @Author: linKe
 * @Date: 2022/1/12 10:43
 * @FileName: SendController
 * @version: 1.0
 * @Description: 消息生产者 发送端
 */
@RestController
public class SendController {
    @Autowired
    RabbitTemplate rabbitTemplate;


    @RequestMapping("/sendEmail")
    public String sendEmail() {
        // 发送邮件
        rabbitTemplate.convertAndSend("exchangeTopic", "email", "email",
                new MessagePostProcessor() {
                    /**
                     * 设置消息属性
                     *
                     * @param message 框架底层创建（类似二阶段的时候 调用HttpServlet）
                     * @return 结果
                     */
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration("10000");
                        message.getMessageProperties().setHeader("email", "测试邮件");
                        message.getMessageProperties().setHeader("email2", "测试邮件2");
                        return message;
                    }
                });
        return "sendEmail";
    }
    @RequestMapping("/sendStudent")
    public String sendStudent() {
        // 发送邮件     发送对象
        rabbitTemplate.convertAndSend("exchangeTopic", "email", new Student(1,"",new Date()),
                new MessagePostProcessor() {
                    /**
                     * 设置消息属性
                     *
                     * @param message 框架底层创建（类似二阶段的时候 调用HttpServlet）
                     * @return 结果
                     */
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        /// message.getMessageProperties().setExpiration("10000");
                        message.getMessageProperties().setHeader("email", "测试邮件");
                        message.getMessageProperties().setHeader("email2", "测试邮件2");

                        // 发送到延迟队列的消息也要设置延迟属性
                        message.getMessageProperties().setDelay(10000);
                        return message;
                    }
                });
        return "sendEmail";
    }

    @RequestMapping("/sendSms")
    public String sendSms() {
        // 发送短信
        rabbitTemplate.convertAndSend("exchangeTopic", "sms", "sms");
        return "sendSms";
    }

    @RequestMapping("/sendAll")
    public String sendAll() {
        // 同时发送邮件和短信
        rabbitTemplate.convertAndSend("exchangeTopic", "sms.email", "sms.email");
        return "sendAll";
    }

    @RequestMapping("/deadLx")
    public String deadLx(){
        // 死信队列测试
        for (int i = 0; i < 50; i++) {
            rabbitTemplate.convertAndSend("exchangeTrue", "deadLx", "死信队列测试");
        }

        return "sendAll";
    }
}
